package com.ebt.m.view.timeline;

import android.content.Context;
import android.text.TextUtils;
import android.text.format.Time;
import android.text.method.LinkMovementMethod;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.customer.net.json.CustomerDynamicJson;
import com.ebt.m.proposal_v2.utils.ProposalUtils;
import com.ebt.m.utils.ConfigData;
import com.ebt.m.utils.CustomerActionUtil;
import com.ebt.m.utils.DateUtils;

public class InteractionViewProposalSend extends IView implements View.OnClickListener, IDynamicJson {

    private TextView mTimeView;
    private TextView mContentView;
    private TextView mDateView;

    public InteractionViewProposalSend(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.view_interaction_item_proposal_send, this);
        mTimeView = (TextView) findViewById(R.id.tv_time);
        mDateView = (TextView) findViewById(R.id.tv_date);
        mContentView = (TextView) findViewById(R.id.tv_content);
        setOnClickListener(this);
    }

    /*private void gotoProposal(){
        if (mJson != null && mJson.objectFromActiveJson != null && mJson.objectFromActiveJson.proposalId != null){
            if (!TextUtils.isEmpty(mJson.objectFromActiveJson.proposalId)) {
                Intent intent = new Intent(getContext(), WebViewActivity.class);
                if (mJson.objectFromActiveJson.proposalUrl != null) {
                    intent.putExtra(WebViewActivity.URLLINK, mJson.objectFromActiveJson.proposalUrl);
                }
                getContext().startActivity(intent);
            }
        }
    }*/

    private void gotoProposal() {
        if (mJson != null && mJson.objectFromActiveJson != null && mJson.objectFromActiveJson.proposalId != null) {
            if (!TextUtils.isEmpty(mJson.objectFromActiveJson.proposalId)) {
//                Intent intent = new Intent(getContext(), WebViewActivity.class);
                if (mJson.objectFromActiveJson.proposalUrl != null) {
//                    intent.putExtra(WebViewActivity.URLLINK, mJson.objectFromActiveJson.proposalUrl);

//                    Intent webIntent = new Intent(getContext(), WebBaseActivity.class);
//                    Bundle bundle = new Bundle();
//                    bundle.putString(WebBaseActivity.WEB_TYPE_TITLE, "计划书");
//                    bundle.putInt(WebBaseActivity.WEB_TYPE_FRAGMENT, WebBaseActivity.FRAGMENT_4NEW_BASE);
//                    bundle.putBoolean(WebBaseActivity.WEB_TYPE_HIDE_TOOLBAR, false);
//                    bundle.putString(WebBaseActivity.WEB_TYPE_LINK, mJson.objectFromActiveJson.proposalUrl);
//                    webIntent.putExtras(bundle);
//                    getContext().startActivity(webIntent);

                    ProposalUtils.previewProposalWithPostFlag(getContext(),
                            mJson.objectFromActiveJson.proposalId,
                            mJson.objectFromActiveJson.proposalName,
                            mJson.objectFromActiveJson.proposalUrl);

                }
//                getContext().startActivity(intent);
            }
        }
    }

    public InteractionViewProposalSend(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InteractionViewProposalSend(Context context) {
        this(context, null);
    }

    @Override
    public void update(Object... obj) {
    }

    @Override
    public void onClick(View v) {
        gotoProposal();
    }

    private CustomerDynamicJson mJson;

    @Override
    public void updateByJson(Object o) {
        if (o instanceof CustomerDynamicJson) {
            CustomerDynamicJson json = (CustomerDynamicJson) o;
            mJson = json;
            try {
                Time time = new Time(Time.getCurrentTimezone());
                time.set(DateUtils.parseTimestampInSecond(json.activeTime));
                mTimeView.setText(time.hour + ":" + ((time.minute + "").length() < 2 ? "0" + time.minute : time.minute));
                if (json.showDate != null && json.showDate) {
                    mDateView.setText((time.month + 1) + "-" + time.monthDay + " " + ConfigData.WEEKSTRS[time.weekDay]);
                }
                updateActContainer(json, json.activeType);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void updateActContainer(CustomerDynamicJson json, int activeType) {
        mContentView.setMovementMethod(new LinkMovementMethod());
        CustomerActionUtil.setTextWithSpanForDyJson2(getContext(), json, activeType, mContentView);
    }

}
